<?
/*
Chương trình: "Quản lý Hóa Đơn và in hóa đơn Trường Thành"
Tác giả: "Lê Phương"
Phiên bản: "1.0"
Tệp tin: "post.inc.php"
*/
if (isset($_GET['print_submit'])) 
{
	require_once('../includes/pdf/config/lang/eng.php');
	require_once('../includes/pdf/tcpdf.php');
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$pdf->setCreator(PDF_CREATOR);
	$pdf->setAuthor('Le Hong Phuong');
	$pdf->setTitle('To GCS');
	$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
	$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
	$pdf->setPrintHeader(false);
	$pdf->setPrintFooter(false);
	$pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
	$pdf->setHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->setFooterMargin(PDF_MARGIN_FOOTER);
	$pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
	$pdf->setLanguageArray($l);

	$timezone  = +7;
	$date=gmdate("Y-m-d h:i:s", time() + 3600*($timezone+date("0")));
	//$date=date("Y-m-d h:i:s");
	$cid = $_GET['id']; 
	$name = $_GET['name']; 
	$name1 = $_GET['name1']; 
	$address = strip_tags(trim($_GET['address']));
	$tax = strip_tags(trim($_GET['tax']));
	$form = strip_tags(trim($_GET['form']));
	$serial = strip_tags(trim($_GET['serial']));
	$soquyen = $_GET['soquyen'];
	$quyenso = $_GET['quyenso'];
	$tuso = $_GET['tuso'];
	$denso = $_GET['denso'];
	$tuquyen = $_GET['tuquyen'];
	$denquyen = $_GET['denquyen'];
	$cname = $name.' '.$name1;
	if($quyenso!=0)
	{
		$i=$quyenso;
		include('includes/switch.inc.php');
		include('template/print.tpl.php');
	}
	elseif($tuso!=0)
	{
		if($tuso%2!=1){echo 'Số hóa đơn trước phải là số lẻ 1 (VD: 1,51,101,151,201)!<br>';}
		elseif($tuso >= $denso){echo 'Làm gì có chuyện số hóa đơn trước lại to hơn số hóa đơn sau!';}
		else
		{
		$i=($tuso+49)/50;
		$k=$denso/50;
		for($i;$i<=$k;$i++)
		{
			include('includes/switch.inc.php');
			include('template/print.tpl.php');
		}
		}
	}
	elseif($tuquyen!=0)
	{
		if($tuquyen >= $denquyen){echo 'Làm gì có chuyện số quyển trước lại to hơn số quyển sau!';}
		else
		{
		$i=$tuquyen;
		$k=$denquyen;
		for($i;$i<=$k;$i++)
		{
			include('includes/switch.inc.php');
			include('template/print.tpl.php');
		}
		}
	}
	else
	{
		for($i=1;$i<=$soquyen;$i++)
		{
			include('includes/switch.inc.php');
			include('template/print.tpl.php');
		}
	}
	$query=mysql_query("INSERT INTO `cover` (`cid`, `soquyen`, `quyenso`, `tuquyen`, `denquyen`, `tuso`, `denso`, `date`) VALUE ('$cid', '$soquyen', '$quyenso', '$tuquyen', '$denquyen', '$tuso', '$denso', '$date')")or die("70 on post.inc.1php");
	ob_end_clean();
	$pdf->Output('tytyty.pdf', 'I');
}
elseif (isset($_GET['save_submit'])) 
{
	$date=date("Y-m-d h:i:s");
	$name = $_GET['name']; 
	$name1 = $_GET['name1']; 
	$address = strip_tags(trim($_GET['address']));
	$tax = strip_tags(trim($_GET['tax']));
	$form = strip_tags(trim($_GET['form']));
	$serial = strip_tags(trim($_GET['serial']));
	$soquyen = $_GET['soquyen'];
	$quyenso = $_GET['quyenso'];
	$tuso = $_GET['tuso'];
	$denso = $_GET['denso'];
	$tuquyen = $_GET['tuquyen'];
	$denquyen = $_GET['denquyen'];
	$file_name = $name.' '.$name1;
	function vn_str_filter ($str){  
        $unicode = array(  
            'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',  
            'd'=>'đ',  
            'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',  
            'i'=>'í|ì|ỉ|ĩ|ị',  
            'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',  
            'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',  
            'y'=>'ý|ỳ|ỷ|ỹ|ỵ',  
            'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',  
            'D'=>'Đ',  
            'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',  
            'I'=>'Í|Ì|Ỉ|Ĩ|Ị',  
            'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',  
            'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',  
            'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',  
        );  
          
       foreach($unicode as $nonUnicode=>$uni){  
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);  
       }
        return $str;
    }  
	$file_name = vn_str_filter($file_name).'.doc';
	header('Content-type: application/doc'); 
	header('Content-disposition: attachment; filename='.$file_name); 
	$cname = $name.' '.$name1;
	include('includes/switch.inc.php');
	include('template/print.tpl.php');
	$query=mysql_query("INSERT INTO `cover` (`cid`, `soquyen`, `quyenso`, `tuquyen`, `denquyen`, `tuso`, `denso`, `date`) VALUE ('$cid', '$soquyen', '$quyenso', '$tuquyen', '$denquyen', '$tuso', '$denso', '$date')")or die("117 on post.inc.php");
}
else
{
	echo 'Không hợp lệ!';
}
?>
